package controller

import (
	"errors"
	"fmt"
	"github.com/e421083458/golang_common/lib"
	"github.com/gin-gonic/gin"
	"tino_manager/dao"
	"tino_manager/dto"
	"tino_manager/middleware"
)

type ActivityController struct {
}

func ActivityRegister(group *gin.RouterGroup) {
	activity := &ActivityController{}
	//创建通知
	group.POST("/add", activity.Add)
	//通知列表
	group.GET("/page", activity.Page)
	//普通用户获取通知列表
	group.GET("/notice_page", activity.NoticePage)
	//获取通知详情
	group.GET("/detail", activity.Detail)
	//修改通知详情
	group.POST("/update", activity.Update)
	//修改通知状态
	group.POST("/update_status", activity.UpdateStatus)
	//删除公告
	group.POST("/delete", activity.Delete)
}

func (n *ActivityController) Add(c *gin.Context) {
	params := &dto.ActivityAddInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败4"), true)
		return
	}
	active := &dao.Activity{
		Title:   params.Title,
		Type:    params.Type,
		Source:  params.Source,
		Link:    params.Link,
		Images:  []byte(params.Images),
		Text:    params.Text,
		Content: params.Content,
		Sort:    params.Sort,
	}
	active.InitData(tx)
	if err := active.Save(c, tx); err != nil {
		fmt.Println(err)
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	middleware.ResponseSuccess(c, "操作成功", true)
}
func (n *ActivityController) Page(c *gin.Context) {
	params := &dto.ActivityPageInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	total := int64(0)
	offset := (params.PageNumber - 1) * params.PageSize
	var list []dao.Activity
	query := tx.WithContext(c).Model(&dao.Activity{}).Where("is_delete=0").Order("sort asc")
	if params.Type == "1" {
		query = query.Where("status = 0")
	}
	if params.Type == "2" {
		query = query.Where("status = 1")
	}
	if params.Type == "3" {
		query = query.Where("status = 2")
	}
	if params.Title != "" {
		query = query.Where("title LIKE ?", "%"+params.Title+"%")
	}
	query.Count(&total)
	if err := query.Limit(params.PageSize).Offset(offset).Find(&list).Error; err != nil {
		middleware.ResponseError(c, 500, errors.New("获取数据失败"), true)
		return
	}
	out := &dto.ActivityPageOutput{
		Total: total,
	}
	for _, item := range list {
		out.List = append(out.List, dto.ActivityInfo{
			Id:        fmt.Sprintf("%d", item.Id),
			Title:     item.Title,
			Type:      item.Type,
			Source:    item.Source,
			Link:      item.Link,
			Text:      item.Text,
			Images:    string(item.Images),
			Content:   item.Content,
			Sort:      item.Sort,
			Status:    item.Status,
			CreatedAt: item.CreatedAt.Format("2006-01-02 15:04:05"),
		})
	}
	middleware.ResponseSuccess(c, out, false)
}
func (n *ActivityController) NoticePage(c *gin.Context) {
	params := &dto.ActivityPageInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	total := int64(0)
	offset := (params.PageNumber - 1) * params.PageSize
	var list []dao.Activity
	query := tx.WithContext(c).Model(&dao.Activity{}).Where("is_delete=0 AND status <> 1").Order("sort asc")
	if params.Title != "" {
		query = query.Where("title LIKE ?", "%"+params.Title+"%")
	}
	query.Count(&total)
	if err := query.Limit(params.PageSize).Offset(offset).Find(&list).Error; err != nil {
		middleware.ResponseError(c, 500, errors.New("获取数据失败"), true)
		return
	}
	out := &dto.ActivityPageOutput{
		Total: total,
	}
	for _, item := range list {
		out.List = append(out.List, dto.ActivityInfo{
			Id:        fmt.Sprintf("%d", item.Id),
			Title:     item.Title,
			Type:      item.Type,
			Source:    item.Source,
			Link:      item.Link,
			Text:      item.Text,
			Images:    string(item.Images),
			Content:   item.Content,
			Sort:      item.Sort,
			Status:    item.Status,
			CreatedAt: item.CreatedAt.Format("2006-01-02 15:04:05"),
		})
	}
	middleware.ResponseSuccess(c, out, false)
}
func (n *ActivityController) Detail(c *gin.Context) {
	params := &dto.ActivityDetailInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("获取数据失败"), true)
		return
	}
	notice := &dao.Activity{}
	if result := tx.Where(map[string]interface{}{
		"id": params.Id,
	}).First(notice); result.RowsAffected == 0 {
		middleware.ResponseError(c, 500, errors.New("获取详情失败"), true)
		return
	}
	middleware.ResponseSuccess(c, dto.ActivityInfo{
		Id:        fmt.Sprintf("%d", notice.Id),
		Title:     notice.Title,
		Type:      notice.Type,
		Source:    notice.Source,
		Link:      notice.Link,
		Images:    string(notice.Images),
		Content:   notice.Content,
		Text:      notice.Text,
		Sort:      notice.Sort,
		Status:    notice.Status,
		CreatedAt: notice.CreatedAt.Format("2006-01-02 15:04:05"),
	}, false)
}
func (n *ActivityController) Update(c *gin.Context) {
	params := &dto.ActivityUpdateInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	notice := &dao.Activity{
		Title:   params.Title,
		Type:    params.Type,
		Source:  params.Source,
		Link:    params.Link,
		Text:    params.Text,
		Images:  []byte(params.Images),
		Content: params.Content,
		Sort:    params.Sort,
	}
	if result := tx.Model(&dao.Activity{}).Where("id = ?", params.Id).Select("title", "sort", "type", "text", "source", "link", "images", "content", "sort").Updates(notice); result.Error != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	middleware.ResponseSuccess(c, "操作成功", true)
}
func (n *ActivityController) UpdateStatus(c *gin.Context) {
	params := &dto.ActivityUpdateStatusInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	if result := tx.Model(&dao.Activity{}).Where("id = ?", params.Id).Update("status", params.Status); result.Error != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	middleware.ResponseSuccess(c, "操作成功", true)
}
func (n *ActivityController) Delete(c *gin.Context) {
	params := &dto.ActivityUpdateStatusInput{}
	if err := params.BindValidParam(c); err != nil {
		middleware.ResponseError(c, 500, err, true)
		return
	}
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	if result := tx.Model(&dao.Activity{}).Where("id = ?", params.Id).Update("is_delete", 1); result.Error != nil {
		middleware.ResponseError(c, 500, errors.New("操作失败"), true)
		return
	}
	middleware.ResponseSuccess(c, "操作成功", true)
}
